﻿using SuperX.Common.Abstract;
using SuperX.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperX.DataService.Abstract;
using SuperX.DataService;
using Newtonsoft.Json.Linq;

namespace SuperX.DeviceService
{
    public class DataHelper
    {
        public static Dictionary<string, object> TagSConvert(Dictionary<string, IIOTag> tags, string tagName, object value)
        {
            var res = new Dictionary<string, object>();
            if (!tags.ContainsKey(tagName))
            {
                return res;
            }
            var tag = tags[tagName];
            var ioTag = tag as IIOTag;
            if (ioTag == null)
            {
                return res;
            }
            var dt = DateTime.Now;
            switch (ioTag.Address.VarType)
            {
                case DataType.STR:
                    ioTag.Value = value.ToString();
                    break;

                case DataType.BOOL:
                    Boolean.TryParse(value.ToString(), out var v);
                    ioTag.Value = v;
                    break;

                case DataType.SHORT:
                    short.TryParse(value.ToString(), out var shortV);
                    ioTag.Value = shortV;
                    break;

                case DataType.WORD:
                    ushort.TryParse(value.ToString(), out var ushortV);
                    ioTag.Value = ushortV;
                    break;

                case DataType.INT:
                    ushort.TryParse(value.ToString(), out var intV);
                    ioTag.Value = intV;
                    break;

                case DataType.DWORD:
                    UInt32.TryParse(value.ToString(), out var uintV);
                    ioTag.Value = uintV;
                    break;

                case DataType.FLOAT:
                    float.TryParse(value.ToString(), out var floatV);
                    ioTag.Value = floatV;

                    break;
            }
            res[ioTag.FullName] = ioTag;
            return res;
        }

        public static Dictionary<string, object> TagSConvert(Dictionary<string, IIOTag> tags, Dictionary<string, object> valuePairs)
        {
            var res = new Dictionary<string, object>();
            foreach (var item in valuePairs)
            {
                var value = item.Value;
                if (!tags.ContainsKey(item.Key))
                {
                    continue;
                }
                var tag = tags[item.Key];
                var ioTag = tag as IIOTag;
                if (ioTag == null)
                {
                    continue;
                }
                var dt = DateTime.Now;
                switch (ioTag.Address.VarType)
                {
                    case DataType.STR:
                        ioTag.Value = value.ToString();

                        break;

                    case DataType.BOOL:
                        Boolean.TryParse(value.ToString(), out var v);
                        ioTag.Value = v;
                        break;

                    case DataType.SHORT:
                        short.TryParse(value.ToString(), out var shortV);
                        ioTag.Value = shortV;
                        break;

                    case DataType.WORD:
                        ushort.TryParse(value.ToString(), out var ushortV);
                        ioTag.Value = ushortV;
                        break;

                    case DataType.INT:
                        ushort.TryParse(value.ToString(), out var intV);
                        ioTag.Value = intV;
                        res[ioTag.FullName] = ioTag;
                        break;

                    case DataType.DWORD:
                        UInt32.TryParse(value.ToString(), out var uintV);
                        ioTag.Value = uintV;
                        break;

                    case DataType.FLOAT:
                        float.TryParse(value.ToString(), out var floatV);
                        ioTag.Value = floatV;
                        break;
                }
                res[ioTag.FullName] = value;
            }
            return res;
        }
    }
}